package com.ruibo.tax.model.dz;


import com.ruibo.tax.model.HBJHXKZJBXX;
import com.ruibo.tax.model.base.PackageHead;
import org.hibernate.collection.internal.PersistentSet;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.lang.reflect.Field;
import java.nio.charset.Charset;

import java.util.Set;

@XmlRootElement(name = "Package")
public class SSDZXXXMLEncodeModel {

    private PackageHead heads = null;//XML报文头

    private SSDZXX roots = null;//XML报文数据

    public void setHeads(PackageHead heads) {
        this.heads = heads;
    }

    public void setRoots(SSDZXX roots) {
        this.roots = roots;
    }

    @XmlElement(name = "PackageHead")
    public PackageHead getHeads() {
        return heads;
    }


    @XmlElement(name = "Data")
    public SSDZXX getRoots() {
        return roots;
    }

    /**
     * 产生模型对应的XML数据
     *
     * @param charset 编码
     * @return String
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    public String toSendData(Charset charset) throws IllegalArgumentException, IllegalAccessException {
        StringBuilder builder = new StringBuilder();
        builder.append("<?xml version=\"1.0\" encoding=\"").append(charset.displayName()).append("\"?>");
        builder.append("<Package>");
        builder.append("<PackageHead>");
        Class<?> headsclz = heads.getClass();
        Field[] headsfields = headsclz.getDeclaredFields();
        for (Field field : headsfields) {
            field.setAccessible(true);
            if (field.get(heads) != null) {
                builder.append("<").append(field.getName()).append(">");
                builder.append((String) field.get(heads));
                builder.append("</").append(field.getName()).append(">");
            } else {
                System.out.println("XML报文头字段：" + field.getName() + " 为空");
            }
        }
        builder.append("</PackageHead>");
        builder.append("<Data>");
        Class<?> aClass = roots.getClass();
        Field[] rootsFields = aClass.getDeclaredFields();
        for (Field field : rootsFields) {
            field.setAccessible(true);
            if (field.get(roots) != null) {
                if (field.getType().equals(Set.class)) {
                    builder.append("<").append(field.getName()).append(">");
                    String encodeModel = encodeModel((PersistentSet) field.get(roots));
                    builder.append(encodeModel);
                    builder.append("</").append(field.getName()).append(">");
                    continue;
                } else {
                    builder.append("<").append(field.getName()).append(">");

                    builder.append(field.get(roots).toString());
                    builder.append("</").append(field.getName()).append(">");
                }
            } else {
                builder.append("<").append(field.getName()).append(">");
                builder.append("</").append(field.getName()).append(">");
            }
        }

        builder.append("</Data>");
        builder.append("</Package>");
        return builder.toString();
    }

    private String encodeModel(PersistentSet dataSet) throws IllegalAccessException {
        StringBuilder stringBuilder = new StringBuilder();
        dataSet.stream().forEach(o -> {
            String name = o.getClass().getSimpleName();
            stringBuilder.append("<").append(name).append(">");
            Class<?> aClass = o.getClass();
            Field[] rootsFields = aClass.getDeclaredFields();
            for (Field field : rootsFields) {
                field.setAccessible(true);
                try {
                    if (field.get(o) != null) {
                        if (field.getType().equals(HBJHXKZJBXX.class)) {
                            continue;
                        } else {
                            stringBuilder.append("<").append(field.getName()).append(">");
                            stringBuilder.append(field.get(o).toString());
                            stringBuilder.append("</").append(field.getName()).append(">");
                        }
                    } else {
                        System.out.println("XML报文数据字段：" + field.getName() + " 为空");
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            stringBuilder.append("</").append(name).append(">");
        });
        return stringBuilder.toString();
    }
}
